package team2.game.crow;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;

public class MainMenu extends BaseGameActivity implements IOnSceneTouchListener{

	
	// ===========================================================
	// Constants
	// ===========================================================
	
	private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 720;

	// ===========================================================
	// Fields
	// ===========================================================
	
	private Scene scene;
	private Scene levelSelect;
	private Camera camera;
	private Font font;
	private TextureRegion newGame;
	//private Text loadGame;
	private TextureRegion levelSelectText;
	private BitmapTextureAtlas spriteBox;
	private Texture fontBox;
	private TextureRegion backgroundTexture;
	private TextureRegion levelSelectBackgroundTexture;
	private TiledTextureRegion levelIcons;
	private Sprite background;
	private Sprite newGameSprite;
	
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public Engine onLoadEngine() {
		
		this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
        engineOptions.getTouchOptions().setRunOnUpdateThread(true);
        return new Engine(engineOptions);
	}

	@Override
	public void onLoadResources() {
		//Sprites
		this.spriteBox = new BitmapTextureAtlas(1024,1024);
		this.backgroundTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spriteBox, this, "MenuBackground.png", 0, 0);
		this.newGame = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spriteBox, this, "NewGame.png", 0, 722);
		this.levelSelectText = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spriteBox, this, "LevelSelect..png", 0, 828);
		this.levelIcons = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "LevelButtonProto.png", 310, 722,5,2);
		this.levelSelectBackgroundTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spriteBox, this, "LevelSelectBackground.png", 481, 0);
		this.mEngine.getTextureManager().loadTexture(this.spriteBox);
		//Fonts
		this.fontBox = new BitmapTextureAtlas(512,512);
		this.font = FontFactory.createFromAsset(this.fontBox, this, "Zipper blues Black.ttf", 50, true, Color.WHITE);
		this.mEngine.getTextureManager().loadTexture(this.fontBox);
    	this.mEngine.getFontManager().loadFont(this.font);
    	
		//Other
	}

	@Override
	public Scene onLoadScene() {
		this.scene = new Scene(1);
		
		//Background
		this.background = new Sprite(0, 0, backgroundTexture);
		this.scene.getFirstChild().attachChild(background);
		
		//Sprites
		this.newGameSprite = new Sprite(100, 500, newGame){
			@Override
        	public boolean onAreaTouched(TouchEvent event,float x, float y){
        		switch(event.getAction()){
        		case TouchEvent.ACTION_DOWN:
        			pickLevel("0");
        			break;
        		}
        		return true;
        	}
		};
		this.scene.getFirstChild().attachChild(newGameSprite);
		this.scene.registerTouchArea(newGameSprite);
		
		
        Sprite levelSelectSprite = new Sprite(100, 610, levelSelectText){
        	@Override
        	public boolean onAreaTouched(TouchEvent event, float x, float y){
        		switch(event.getAction()){
        		case TouchEvent.ACTION_DOWN:
        			levelSelect();
        			break;
        		}
        		return true;
        	}
        };
        this.scene.getFirstChild().attachChild(levelSelectSprite);
        this.scene.registerTouchArea(levelSelectSprite);
        
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}
	
	public void onResume(){
		super.onResume();
		this.getEngine().setScene(scene);
		if(this.scene != null)
			this.scene.setIgnoreUpdate(false);
	}
	
	@Override
	public void onBackPressed(){
		if(this.getEngine().getScene() != this.scene){
			this.getEngine().setScene(scene);
		}
		else{
			this.finish();
		}
	}
	
	@Override
	public void onNewIntent(Intent i){
		this.getEngine().setScene(scene);
		this.scene.setIgnoreUpdate(false);
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		return false;
	}
	// ===========================================================
	// Methods
	// ===========================================================
	
	private void levelSelect(){
		levelSelect = new Scene();
		
		Debug.d("The levelSelect Reached.");
		
		Sprite levelSelectBackground = new Sprite(0,0, levelSelectBackgroundTexture);
		levelSelect.attachChild(levelSelectBackground);
		
		AnimatedSprite level1 = new AnimatedSprite(100, 20, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("0");
					break;
				}
				return true;
			}
		};
		level1.stopAnimation(0);
		levelSelect.attachChild(level1);
		levelSelect.registerTouchArea(level1);
		AnimatedSprite level2 = new AnimatedSprite(300, 20, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("1");
					break;
				}
				return true;
			}
		};
		level2.stopAnimation(1);
		levelSelect.attachChild(level2);
		levelSelect.registerTouchArea(level2);
		AnimatedSprite level3 = new AnimatedSprite(100, 160, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("2");
					break;
				}
				return true;
			}
		};
		level3.stopAnimation(2);
		levelSelect.attachChild(level3);
		levelSelect.registerTouchArea(level3);
		AnimatedSprite level4 = new AnimatedSprite(300, 160, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("3");
					break;
				}
				return true;
			}
		};
		level4.stopAnimation(3);
		levelSelect.attachChild(level4);
		levelSelect.registerTouchArea(level4);
		AnimatedSprite level5 = new AnimatedSprite(100, 300, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("4");
					break;
				}
				return true;
			}
		};
		level5.stopAnimation(4);
		levelSelect.attachChild(level5);
		levelSelect.registerTouchArea(level5);
		AnimatedSprite level6 = new AnimatedSprite(300, 300, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("5");
					break;
				}
				return true;
			}
		};
		level6.stopAnimation(5);
		levelSelect.attachChild(level6);
		levelSelect.registerTouchArea(level6);
		AnimatedSprite level7 = new AnimatedSprite(100, 440, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("6");
					break;
				}
				return true;
			}
		};
		level7.stopAnimation(6);
		levelSelect.attachChild(level7);
		levelSelect.registerTouchArea(level7);
		AnimatedSprite level8 = new AnimatedSprite(300, 440, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("7");
					break;
				}
				return true;
			}
		};
		level8.stopAnimation(7);
		levelSelect.attachChild(level8);
		levelSelect.registerTouchArea(level8);
		AnimatedSprite level9 = new AnimatedSprite(100, 580, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("8");
					break;
				}
				return true;
			}
		};
		level9.stopAnimation(8);
		levelSelect.attachChild(level9);
		levelSelect.registerTouchArea(level9);
		AnimatedSprite level10 = new AnimatedSprite(300, 580, levelIcons.deepCopy()){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					pickLevel("9");
					break;
				}
				return true;
			}
		};
		level10.stopAnimation(9);
		levelSelect.attachChild(level10);
		levelSelect.registerTouchArea(level10);
			
		this.scene.setIgnoreUpdate(true);
		this.getEngine().setScene(levelSelect);		
	}
	
	private void pickLevel(String levelNumber){
		Uri.Builder urib = new Uri.Builder();
		urib.appendPath(levelNumber);
		Uri level = urib.build();
		Intent i = new Intent(MainMenu.this, CrowActivity.class);
		i.setData(level);
        startActivity(i);
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
